package com.project.webpage.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.project.webpage.dto.*;
import com.project.webpage.entity.Gjxl;
import com.project.webpage.entity.Xlgjday;
import com.project.webpage.mapper.*;
import com.project.webpage.service.XlbcGpsService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class XlbcGpsServiceImpl implements XlbcGpsService {

    @Resource
    private XlbcGpsMapper xlbcGpsMapper;
    @Resource
    private CzjczRMapper czjczRMapper;
    @Resource
    private GjxlMapper gjxlMapper;
    @Resource
    private XlgjdayMapper xlgjdayMapper;
    @Resource
    private SkbDailyMapper skbDailyMapper;

    @Override
    @DS("master")
    public List<ShBcDto> getListForPage(SearchByRqParam param) {
        return xlbcGpsMapper.selectListByUid(param);
    }

    @Override
    @DS("master")
    public List<ShBcDto> getQyListForPage(SearchByRqParam param) {
        return xlbcGpsMapper.selectQyListByUid(param);
    }

    @Override
    @DS("master")
    public List<ShPcDto> getPcListForPage(SearchByRqParam param) {
        return xlbcGpsMapper.selectPcListByUid(param);
    }

    @Override
    @DS("master")
    public List<ShPcDto> getPcQyListForPage(SearchByRqParam param) {
        return xlbcGpsMapper.selectPcQyListByUid(param);
    }

    @Override
    @DS("master")
    public List<GpsCphDto> getPcCphListForPage(SearchByRqParam param) {
        return xlbcGpsMapper.selectPcCphListByUid(param);
    }

    @Override
    @DS("master")
    public List<ShPjsdDto> getPjsdListForPage(ShPjsdParam param) {
        return xlbcGpsMapper.selectPjsdListByUid(param);
    }

    @Override
    @DS("master")
    public List<ShZzDto> getZzListForPage(ShZzParam param) {
        return czjczRMapper.selectListByUid(param);
    }

    @Override
    @DS("master")
    public List<GpsCphDto> getGpsPcListForPage(GpsPcBcParam param) {
        List<GpsCphDto> list = new ArrayList<>();
        String rqtime = "";
        Example example0 = new Example(Gjxl.class);
        Example.Criteria c0 = example0.createCriteria();
        c0.andEqualTo("jgxlid", param.getJgxlid());
        c0.andEqualTo("state", 0);
        List<Gjxl> gjxlList = gjxlMapper.selectByExample(example0);
        if (gjxlList.size() > 0) {
            if (gjxlList.get(0).getYysj() == 2) {
                rqtime = param.getRq() + " 15:00:00";
            } else {
                rqtime = param.getRq() + " 03:00:00";
            }
            if (param.getType() == 0) {
                Example example = new Example(Xlgjday.class);
                Example.Criteria c = example.createCriteria();
                c.andEqualTo("rq", param.getRq());
                c.andEqualTo("jgxlid", param.getJgxlid());
                List<Xlgjday> xlgjdayList = xlgjdayMapper.selectByExample(example);
                if (xlgjdayList.size() > 0) {
                    param.setRq(rqtime);
                    if ("GPS_RFID".equals(xlgjdayList.get(0).getJs_mode())) {
                        list = xlbcGpsMapper.selectGpsRfidPcListByJgxlid(param);
                    } else if ("RFID".equals(xlgjdayList.get(0).getJs_mode())) {
                        list = xlbcGpsMapper.selectRfidPcListByJgxlid(param);
                    } else {
                        list = xlbcGpsMapper.selectGpsPcListByJgxlid(param);
                    }
                }
            } else if (param.getType() == 1) {
                param.setRq(rqtime);
                list = xlbcGpsMapper.selectGpsPcListByJgxlid(param);
            } else {
                param.setRq(rqtime);
                list = xlbcGpsMapper.selectRfidPcListByJgxlid(param);
            }
        }
        return list;
    }

    @Override
    @DS("master")
    public GpsBcListDto getGpsBcListForPage(GpsPcBcParam param) {
        GpsBcListDto gpsBcListDto = new GpsBcListDto();
        List<GpsBcDto> list = new ArrayList<>();
        List<GpsBcDto> resultList = new ArrayList<>();
        // 时刻表班次
        List<SkbDetailDto> skbDetailDtoList = skbDailyMapper.selectDetailListByJgxlid(param);
        if (skbDetailDtoList.size() > 0) {
            for (SkbDetailDto skbDetailDto : skbDetailDtoList) {
                GpsBcDto dto = new GpsBcDto();
                dto.setSkbDetail(skbDetailDto);
                list.add(dto);
                resultList.add(dto);
            }
        }
        List<LdxxBcDto> ldxxBcDtoList = new ArrayList<>();
        if (param.getType() == 0) {
            Example example = new Example(Xlgjday.class);
            Example.Criteria c = example.createCriteria();
            c.andEqualTo("rq", param.getRq());
            c.andEqualTo("jgxlid", param.getJgxlid());
            List<Xlgjday> xlgjdayList = xlgjdayMapper.selectByExample(example);
            if (xlgjdayList.size() > 0) {
                if ("GPS_RFID".equals(xlgjdayList.get(0).getJs_mode())) {
                    gpsBcListDto.setType("GPS_RFID");
                } else if ("RFID".equals(xlgjdayList.get(0).getJs_mode())) {
                    gpsBcListDto.setType("RFID");
                } else {
                    gpsBcListDto.setType("GPS");
                }
                ldxxBcDtoList = xlbcGpsMapper.selectGpsRfidBcListByJgxlid(param);
            }
        } else if (param.getType() == 1) {
            gpsBcListDto.setType("GPS");
            ldxxBcDtoList = xlbcGpsMapper.selectGpsBcListByJgxlid(param);
        } else {
            gpsBcListDto.setType("RFID");
            ldxxBcDtoList = xlbcGpsMapper.selectRfidBcListByJgxlid(param);
        }
        if (ldxxBcDtoList.size() > 0) {
            if (list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    SkbDetailDto skbDetailDto = list.get(i).getSkbDetail();
                    List<LdxxBcDto> ldxxBcDtos = ldxxBcDtoList.stream().filter(item -> item.getSxx_int() == skbDetailDto.getSxx_int() && item.getJhfcsj().equals(skbDetailDto.getJhfcsj())).collect(Collectors.toList());
                    if (ldxxBcDtos.size() > 0) {
                        resultList.get(i).setBcDetail(ldxxBcDtos.get(0));
                        ldxxBcDtos.get(0).setIscheck(1);
                        if (ldxxBcDtos.size() > 1) {
                            for (int j = 1; j < ldxxBcDtos.size(); j++) {
                                GpsBcDto dto = new GpsBcDto();
                                dto.setBcDetail(ldxxBcDtos.get(j));
                                if (i == list.size() - 1) {
                                    resultList.add(dto);
                                } else {
                                    resultList.add(i + 1, dto);
                                }
                                ldxxBcDtos.get(j).setIscheck(1);
                            }
                        }
                    }
                }
                // 未匹配数据重新匹配
                List<LdxxBcDto> ldxxBcDtoNochecks = ldxxBcDtoList.stream().filter(item -> item.getIscheck() == null).collect(Collectors.toList());
                if (ldxxBcDtoNochecks.size() > 0) {
                    List<GpsBcDto> newList = new ArrayList<>();
                    for (int i = 0; i < ldxxBcDtoNochecks.size(); i++) {
                        long jhfcTimeNochecks = ldxxBcDtoNochecks.get(i).getJhfcsj_time().getTime();
                        for (int a = 0; a < resultList.size(); a++) {
                            long jhfcTime = resultList.get(a).getSkbDetail().getJhfcsj_time().getTime();
                            long diffInMis = (jhfcTimeNochecks - jhfcTime) / (1000 * 60);
                            if (resultList.get(a).getBcDetail() == null && diffInMis >= -15 && diffInMis <= 15) {
                                resultList.get(a).setBcDetail(ldxxBcDtoNochecks.get(i));
                                ldxxBcDtoNochecks.get(i).setIscheck(1);
                                break;
                            }
                        }
                        if (ldxxBcDtoNochecks.get(i).getIscheck() == null) {
                            if (newList.size() == 0) {
                                Collections.copy(newList, resultList);
                            }
                            GpsBcDto dto = new GpsBcDto();
                            dto.setBcDetail(ldxxBcDtoNochecks.get(i));
                            for (int n = 0; n < newList.size(); n++) {
                                long jhfcTime = newList.get(n).getBcDetail().getJhfcsj_time().getTime();
                                long diffInMis = (jhfcTimeNochecks - jhfcTime) / (1000 * 60);
                                if (diffInMis >= -15 && diffInMis <= 15) {
                                    if (n == newList.size() - 1) {
                                        newList.add(dto);
                                    } else {
                                        newList.add(n + 1, dto);
                                    }
                                    dto.getBcDetail().setIscheck(1);
                                    break;
                                }
                            }
                            if (dto.getBcDetail().getIscheck() == null) {
                                newList.add(dto);
                                dto.getBcDetail().setIscheck(1);
                            }
                        }
                    }
                    if (newList.size() > 0) {
                        Collections.copy(resultList, newList);
                    }
                }
            } else {
                for (LdxxBcDto ldxxBcDto : ldxxBcDtoList) {
                    GpsBcDto dto = new GpsBcDto();
                    dto.setBcDetail(ldxxBcDto);
                    resultList.add(dto);
                }
            }
        }
        gpsBcListDto.setBcList(resultList);
        return gpsBcListDto;
    }
}
